package com.app.server.impl;

import com.app.mapper.UnitsManagerMapper;
import com.app.model.UnitsManagerModel;
import com.app.server.UnitsManagerService;
import com.app.utils.StringTool;
import com.app.vo.UnitsManagerVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UnitsManagerServiceImpl extends ServiceImpl<UnitsManagerMapper, UnitsManagerModel> implements UnitsManagerService {
    @Autowired
    private UnitsManagerMapper unitsManagerMapper;

    //查询单位管理列表
    @Override
    public UnitsManagerVo getUnitsManagerList(Integer page, Integer limit, String name) {
        Page<UnitsManagerModel> page1 = new Page<>(page, limit);
        QueryWrapper<UnitsManagerModel> qw =new QueryWrapper<>();
        if (StringTool.isNotEmpty(name)){
            qw.like("name",name);
        }
        qw.eq("del",0);
        qw.orderByDesc("id");
        Page<UnitsManagerModel> unitsManagerPage = unitsManagerMapper.selectPage(page1, qw);
        List<UnitsManagerModel> unitsManagerList = unitsManagerPage.getRecords();

        long count = unitsManagerPage.getTotal();
        UnitsManagerVo unitsManagerVo = new UnitsManagerVo();
        unitsManagerVo.setList(unitsManagerList);
        unitsManagerVo.setCount((int) count);
        return unitsManagerVo;
    }
    //根据单位名字查询单位对象
    @Override
    public UnitsManagerModel getByName(String name) {
        QueryWrapper<UnitsManagerModel> qw=new QueryWrapper<>();
        qw.eq("name",name);
        qw.eq("del",0);
        UnitsManagerModel unitsManager = unitsManagerMapper.selectOne(qw);
        return unitsManager;
    }
    //获取所有单位名称
    @Override
    public List<String> getAllUnitsName() {
        List<String> allUnitsName = unitsManagerMapper.getAllUnitsName();
        return allUnitsName;
    }
}
